var winSubject,winU, winJ, winH;
$(document).ready(function(){

    SelectArea({ct:'sarea', p:'p', c:'c', d:'d',ps:true});


    var selSubject = $('#selsub');
    var selSchool = $('#sels');

    var dselSubject = $('#dselsub');
    var dselSchool = $('#dsels');

    var teacherType = $('#st');

    dselSubject.click(function(){
        $('#subname').val('');
        $('#sub').val('');
    });

    dselSchool.click(function(){
        $('#schname').val('');
        $('#s').val('');
    });
    selSubject.click(function(){
        if(winSubject == null) {
            winSubject = new Subject(function(a){
                $('#subname').val(a.text());
                $('#sub').val(a.attr('id'));
                winSubject.hide();
            });
        }
        winSubject.show();
    });
    SelectSchedule('sch', true);

    selSchool.click(function(e){
        var val = teacherType.val();
        if(val == '') {
            InfoPanel.show('请先指定教师类型','i', 1000);
            teacherType.focus();
            return;
        }
        var tp = parseInt(val);
        switch (tp) {
            case 0:// 在校大学生
                if (winU == null) {
                    winU = new UnivWin(function(a) {
                        a = $(a);
                        $('#schname').val(a.text());
                        $('#s').val(a.attr('id'));
                        winU.hide();
                    });
                }
                winU.show();
                break;
            case 1://在职高中教师
                if (winH == null) {
                    winH = new HighWin(function(a) {
                        $('#schname').val(a.text());
                        $('#s').val(a.attr('id'));
                        winH.hide();
                    });
                }
                winH.show();
                break;
            case 3://在职初中教师
                if (winJ == null) {
                    winJ = new JuniorWin(function(a) {
                        $('#schname').val(a.text());
                        $('#s').val(a.attr('id'));
                        winJ.hide();
                    });
                }
                winJ.show();
                break;
            }
    });

    $('#search').click(function() {
        doSearch();
    });
});

var searchUrl = path + '/search/tutor';
var tbHd = '';
var tplHTML = '{for i in list}<tr>'+
	'<td><a href="${path}/tutorinfo/detail/${i.id}" target="_blank">${i.name}</a></td>'+
	'<td>${i.province}</td>'+
    '<td>${i.city}</td>'+
    '<td>${i.area}</td>'+
	'<td>${i.school}</td>'+
	'<td>{if i.city.length > 0}${i.city}{else}&nbps;{/if}${i.city}</td>'+
    '<td>${getCourse(i.subject)}</td>'+
    '<td>${pareSchedule(i.schedule)}</td>'+
	'</tr>{/for}';


var tpl = TrimPath.parseTemplate(tplHTML);

function doSearch() {
    var doc = document;
    var params = {};
    var eles = $('input[type=text], input[type=hidden], select');
    var filter = ['subname','schname'];
    eles.each(function(){
        var ele = $(this);
        v = ele.val();
        n = ele.attr('name');
        if(v != '' && filter.indexOf(n) == -1) {
            params[n] = v;
        }
    });
    AjaxRequest(searchUrl, params, function(json) {
        if(json.length > 0 ) {
            var t = $('<table border="1"></table>');
            var s = tpl.process({list:json});
            t.append(s);

            var c  = $('#content');
            c.html('');
            t.appendTo(c);

        } else {
            $('#content').html('There is no result found');
        }
    });
}
